#!/bin/sh
# CC strlst
# hastily written
# but does the job
# whatever

ROOT="$HOME/.quests"
COLS=$(expr $(stty size | cut -d' ' -f 2))

splitline() {
    printf "\n  +"
    printf %-$((COLS - 6))s | tr ' ' '-'
    printf "+  \n"
}

splititem() {
    number='-' && [ ! -z $1 ] && number=$1
    echo
    printf %-$((($COLS - 3) / 2))s
    echo "- ${number} -"
}

printitem() {
    [ $(printf "$*" | wc -c) -gt $(($COLS - 4)) ] && printf "  $*  " && return
    count=$(printf "$*" | wc -c)
    border=$((($COLS - count) / 2))
    printf %-$(($border))s
    echo -n $* | sed 's/_/ /g'
    printf %-$(($border))s
    echo
}

[ $(($(ls -1 "$ROOT" | wc -c))) -lt 2 ] && echo "nothing to do" && exit 0

splitline
# iterate over lines, not words
IFS="
"
index=1
for i in $(ls -1 "$ROOT"); do
    splititem ${index}
    printitem $i
    index=$((${index} + 1))
done
splitline
